<?php


namespace app\common\model;


use think\facade\Route;
use think\Model;

class Files extends Model {
    protected $pk = 'id';
    //附件
    protected $append=['copyurl'];

    public function getCtimeAttr($value,$data){
        return date('Y-m-d H:i:s',$value);
    }

    public function getCopyurlAttr($value,$data){
       return Route::buildUrl('common/download2',['fno'=>$data['fno']])->domain('http://www.ceoyaolan.com/pc')->build();
    }

    public function uploadAppendix($file_type = 'appendix'){

        $result = error_code(10035);
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize  = config('yitong.file_size');
        $filePath = ROOT_PATH . 'public';
        if (!is_dir($filePath)) {
            @mkdir($filePath);
        }

        try {
            validate(
                [
                    'file' => [
                        // 限制文件大小(单位b)，这里限制为4M
                        'fileSize' => $maxSize,
                    ]
                ],
                [
                    'file.fileSize' => '文件太大',
                ]
            )->check(['file' => $file]);

            $savename = \think\facade\Filesystem::disk('public')->putFile( 'appendix', $file);

            $name = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName());
            $extension = substr(strrchr($savename, '.'), 1);
            $url                = '/storage/'.$savename;
            $iData['type']      = 'local';
            $iData['file_type'] = $file_type;
            $iData['name']      = $name.'.'.$extension;
            $iData['url']       = $url;
            $iData['ctime']     = TIMESTAMP;
            $iData['fno']       = md5(date('YmdHis').rand(10000,99999));
            $iData['path']      = $filePath.'/storage/'.$savename;
            if ($this->strict(false)->insert($iData)) {
                $result['data']   = $iData;
                $result['status'] = true;
                $result['msg']    = '上传成功';
                return $result;
            }

        }catch (\think\exception\ValidateException $e){
            // 上传失败获取错误信息
            $result['msg'] = $e->getError();
            return $result;
        }
    }


    //附件
    public function uploadImg($file_type = 'wxappimg'){

        $result = error_code(10035);
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize  = config('yitong.file_size');
        $filePath = ROOT_PATH . 'public';
        if (!is_dir($filePath)) {
            @mkdir($filePath);
        }

        try {
            validate(
                [
                    'file' => [
                        // 限制文件大小(单位b)，这里限制为4M
                        'fileSize' => $maxSize,
                    ]
                ],
                [
                    'file.fileSize' => '文件太大',
                ]
            )->check(['file' => $file]);

            $savename = \think\facade\Filesystem::disk('public')->putFile( 'wxappimg', $file);

            $name = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName());
            $extension = substr(strrchr($savename, '.'), 1);
            $url                = '/storage/'.$savename;
            $iData['type']      = 'local';
            $iData['file_type'] = $file_type;
            $iData['name']      = $name.'.'.$extension;
            $iData['url']       = $url;
            $iData['ctime']     = time();
            $iData['path']      = $filePath.'/storage/'.$savename;
            if ($this->strict(false)->insert($iData)) {
                $result['data']   = $iData;
                $result['status'] = true;
                $result['msg']    = '上传成功';
                return $result;
            }

        }catch (\think\exception\ValidateException $e){
            // 上传失败获取错误信息
            $result['msg'] = $e->getError();
            return $result;
        }
    }

}